package cn.com.inossem.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;

import cn.com.inossem.systembartint.SystemBarTintManager;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class ShareDialogActivity extends Activity {
	TextView back;
	String id, name;
	String shareImage;
	String startDate, projectAddress, projectCycle;
	private File file;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}

		SystemBarTintManager tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setTintColor(getResources().getColor(R.color.app_main));

		setContentView(R.layout.shareactivity_dialog);

		id = getIntent().getStringExtra("id");
		name = getIntent().getStringExtra("name");
		startDate = getIntent().getStringExtra("startDate");
		projectAddress = getIntent().getStringExtra("projectAddress");
		projectCycle = getIntent().getStringExtra("projectCycle");
		initImage();
		ImageView shareweixin = (ImageView) this.findViewById(R.id.shareweixin);
		shareweixin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// finish();
				showShare();
			}
		});
		ImageView sharemail = (ImageView) this.findViewById(R.id.sharemail);
		sharemail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				//				String url_share = "http://123.57.137.89";
				String url_share = "http://sapsvr06.chinacloudapp.cn";

				String[] email = {""}; // 需要注意，email必须以数组形式传入
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("message/rfc822"); // 设置邮件格式
				intent.putExtra(Intent.EXTRA_EMAIL, email); // 接收人
				// intent.putExtra(Intent.EXTRA_CC, email); // 抄送人
				intent.putExtra(Intent.EXTRA_SUBJECT, "分享智仁项目机会：" + name); // 主题
				//				intent.putExtra(Intent.EXTRA_TEXT, "<a href=\"" + url_share + "/openSapApp.html?appUrl=wxb85aaba08de1b45b:%2F%2F%3Fproject_id%3d"
				//								+ id + "&appUrlA=m:%2F%2Fb.app%2Fo%3Fid%3d" + id + "\">" + name + "</a>"); // 正文
				intent.putExtra(Intent.EXTRA_TEXT, name + ": " + url_share + "/ZProject/index.html?appUrl=wxb85aaba08de1b45b:%2F%2F%3Fproject_id%3d"
						+ id + "&appUrlA=m:%2F%2Fb.app%2Fo%3Fid%3d" + id + "&project_id=" + id); // 正文
				startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
			}
		});

		back = (TextView) this.findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		//		String url_share = "http://123.57.137.89";
		//		System.out.println("url:"
		//				+ url_share
		//				+ "/openSapApp.html?appUrl=wxb85aaba08de1b45b:%2F%2F%3Fproject_id%3d"
		//				+ id + "&appUrlA=m:%2F%2Fb.app%2Fo%3Fid%3d" + id);
	}

	public void initImage() {
		try {// 判断SD卡中是否存在此文件夹
			//            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
			//                    && Environment.getExternalStorageDirectory().exists()) {
			//                File baseFile = new File(Environment.getExternalStorageDirectory(), "share");
			//                if (!baseFile.exists()) {
			//                    baseFile.mkdir();
			//                }
			//                shareImage = baseFile.getAbsolutePath() + "/sapshare.png";
			//            } else {
			//            	shareImage = getApplication().getFilesDir().getAbsolutePath() + "/sapshare.png";
			//            }
			//            File file = new File(shareImage);
			//            if (file.exists()) {
			//            	file.delete();
			//            }
			file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/zhiren/sapshare.png");
			file.getParentFile().mkdirs();
			file.createNewFile();
			Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.zhi);
			FileOutputStream fos = new FileOutputStream(file);
			pic.compress(CompressFormat.PNG, 50, fos);
			fos.flush();
			fos.close();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private void showShare() {
		/*Bitmap bt = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.zhi);
		Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bt, null, null));
		File file = new File(uri.getPath());
		file.exists();
		String fileName = "android.resource://" + this.getPackageName() + "/drawable-hdpi-v4/zhi.png";
		File file1 = new File(fileName);
		file1.exists();
		Uri introURI = Uri.parse("android.resource://" + this.getPackageName() + "/drawable/" + R.drawable.zhi);  
		File file2 = new File(introURI.getPath());
		file2.exists();*/
		ShareSDK.initSDK(this);
		OnekeyShare oks = new OnekeyShare();
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();

		// // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		// oks.setTitle(getString(R.string.share));
		// // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		// oks.setTitleUrl("http://sharesdk.cn");
		// // text是分享文本，所有平台都需要这个字段
		// oks.setText("我是分享文本http://www.baidu.com");
		// // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		// oks.setImagePath("http://img0.bdstatic.com/img/image/shouye/muzrxc1.jpg");//
		// 确保SDcard下面存在此张图片
		// // url仅在微信（包括好友和朋友圈）中使用
		// // oks.setUrl("m://b.app/o?id=" + id);
		// oks.setUrl("http://www.baidu.com");

		// 分享时Notification的图标和文字 2.5.9以后的版本不调用此方法
		// oks.setNotification(R.drawable.ic_launcher,
		// getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		//		oks.setTitle("分享项目机会：【" + name + "】");
		oks.setTitle("分享智仁项目机会");
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		//		String url_share = "http://123.57.137.89";
		String url_share = "http://zhirenapp.inossem.com";
		oks.setTitleUrl(url_share
				+ "/ZProject/index.html?appUrl=wxb85aaba08de1b45b:%2F%2F%3Fproject_id%3d"
				+ id + "&appUrlA=m:%2F%2Fb.app%2Fo%3Fid%3d" + id + "&project_id=" + id);
		String text = "项目名称：" + name + "\n开始时间：" + startDate + "\n项目周期：" + projectCycle + "\n项目地点：" +
				projectAddress + "";
		oks.setText(text);
		// text是分享文本，所有平台都需要这个字段
		// oks.setText("我是分享文本");
		// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		// oks.setImagePath("/sdcard/share.png");// 确保SDcard下面存在此张图片
		//		Uri uri =  Uri.parse("android.resource://cn.com.inossem.activity/" + R.drawable.zhi);
		oks.setImagePath(file.getAbsolutePath());
		//		oks.setImageUrl("http://img.my.csdn.net/uploads/201404/13/1397393290_5765.jpeg");
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl(url_share
				+ "/ZProject/index.html?appUrl=wxb85aaba08de1b45b:%2F%2F%3Fproject_id%3d"
				+ id + "&appUrlA=m:%2F%2Fb.app%2Fo%3Fid%3d" + id + "&project_id=" + id);
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		//		oks.setComment("我是测试评论文本");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(getString(R.string.app_name));
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		// oks.setSiteUrl("http://sharesdk.cn");

		// 启动分享GUI
		oks.show(this);
	}

	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
}
